﻿//************************************************************************
//* Written by Jirat Boomuang
//* Filename: ApplicationMain.cs
//* Revision: 1.0
//* Author: JiratB
//* Platform: .NET Framework 4.0
//* Description: Main part of the application. The main business logic
//*             is under Run()
//************************************************************************

//************************************************************************
//* Revision History
//------------------------------------------------------------------------
//* Revision  Date      Signature    Description
//------------------------------------------------------------------------
//* 1.0       20131115  JiratB       - First release
//************************************************************************

//-- For Standard Components ---------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

using Microsoft.Crm.Sdk.Messages;
//------------------------------------------------------------------------

namespace CrmSdkConsoleApplication
{
    public class ApplicationMain
    {
        #region Constants

        protected const string CONNECTING_PLEASEWAIT = "\n\n...Connecting to your CRM Organization, please wait...\n";
        protected const string SUCCESSFUL_CONNECTION = "\n...Your session is successfully connected to your CRM Organization...\n";

        #endregion

        #region Data Members

        protected OrganizationServiceProxy serviceProxy;

        #endregion

        #region Run()
        public void Run(ServerConnection.Configuration serverConfig, string[] args)
        {
            try
            {
                Console.WriteLine(CONNECTING_PLEASEWAIT);
                using (serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                {
                    Console.WriteLine(SUCCESSFUL_CONNECTION);

                    //-- Delete the following statements; they are just a dummy code --
                    Console.WriteLine();
                    Console.WriteLine("*******************************************************************************");
                    Console.WriteLine("Hello World!!!");
                    Console.WriteLine();
                    Console.WriteLine("If you see this message, it means that the CRM connection is working");
                    Console.WriteLine("You may refer to 'serviceProxy' as your OrganizationProxy instance in your code");
                    Console.WriteLine();
                    Console.WriteLine("Note: your part of code shall be under 'using (serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)) {}' scope");
                    Console.WriteLine("*******************************************************************************");
                    Console.WriteLine();
                    Console.WriteLine("-- press any key to continue --");                    
                    //-- End of Delete Here -------------------------------------------

                    AdaCreateEntitiesFromFiles adaEntities = new AdaCreateEntitiesFromFiles();
                    adaEntities.CreateEntityFromCSVFile(serviceProxy, true, "m_entities.csv");

                    //Console.ReadKey();
                }//-- End of using() --

            }//-- End of try --
            catch (Exception e)
            {
                Console.WriteLine("\n");
                Console.WriteLine("*******************************************************************************");
                Console.WriteLine("[ERROR]: {0}", e.Message);
                Console.WriteLine("*******************************************************************************");

            }//-- End of catch() --

        }//-- End of Run() --
        #endregion

    }//-- End Of Class ApplicationMain --
}//-- End Of Namespace --

//************************************************************************
// End of file
//************************************************************************
